package com.lxw.rmg.menu.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxw.common.utils.PageUtils;
import com.lxw.common.utils.R;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lxw.rmg.menu.entity.MatterEntity;
import com.lxw.rmg.menu.entity.MenuEntity;
import com.lxw.rmg.menu.entity.PersonDishEntity;
import com.lxw.rmg.menu.service.MatterService;
import com.lxw.rmg.menu.service.MenuService;
import com.lxw.rmg.menu.service.PersonDishService;
import com.mysql.cj.util.StringUtils;
import io.renren.common.utils.ShiroUtils;
import org.apache.commons.lang.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lxw.rmg.menu.entity.SubscribeEntity;
import com.lxw.rmg.menu.service.SubscribeService;



/**
 * 预约管理
 *
 * @author lxw
 * @email 
 * @date 2023-03-21 22:04:39
 */
@RestController
@RequestMapping("menu/subscribe")
public class SubscribeController {
    @Autowired
    private SubscribeService subscribeService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MatterService matterService;
    @Autowired
    private PersonDishService personDishService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("menu:subscribe:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = subscribeService.queryPage(params);

//        //获取参数
//        String dishName = (String)params.get("dishName");
//        if (!StringUtils.isNullOrEmpty(dishName) && !" ".equals(dishName)){
//            List subscribeEntities = subscribeService.list(
//                    new QueryWrapper<SubscribeEntity>().eq("dish_name", dishName));
//            page.setList(subscribeEntities);
//            page.setTotalCount(subscribeEntities.size());
//        }
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("menu:subscribe:info")
    public R info(@PathVariable("id") Integer id){
		SubscribeEntity subscribe = subscribeService.getById(id);

        return R.ok().put("subscribe", subscribe);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("menu:subscribe:save")
    @Transactional
    public R save(@RequestBody SubscribeEntity subscribe){
        String idCard = subscribe.getIdCard();
        //自动生成所需字段-当前时间

        String TimeMillis = String.valueOf(System.currentTimeMillis());
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());

        //获取点菜的菜名
        String dishName = subscribe.getDishName();


        if (StringUtils.isNullOrEmpty(idCard) || " ".equals(idCard)){
            return R.error("请输入身份证");
        }
        //建立关联数据
        PersonDishEntity personDishEntity = new PersonDishEntity();
        personDishEntity.setDishName(dishName);
        personDishEntity.setCreatedAt(nowTime);
        personDishEntity.setIdCard(idCard);
        personDishService.save(personDishEntity);


        subscribe.setSubCode(TimeMillis);
        subscribe.setOrderTime( nowTime);

        //到菜谱查询是否存在
        MenuEntity menu = menuService.getOne(new QueryWrapper<MenuEntity>().eq("dish_name", dishName));
        if (null != menu){
            //获取物料
            String reqMate = menu.getReqMate();
            //查看所需物料
            if (reqMate.contains(",") || reqMate.contains("_")) {
                //分割
                String[] rms = reqMate.split(",");
                //遍历物料
//                for (String rm : rms) {
                for (int i=0;i<rms.length;i++){
                    //获取每一种物料
                    String[] s = rms[i].split("_");
                    //获取物料表
                    MatterEntity matterEntity = matterService.getOne(new QueryWrapper<MatterEntity>().eq("mate_name", s[0]).ge("current_inventory", Integer.parseInt(s[1])));
                    //如果是
                    if (null == matterEntity) {
                        return R.error(s[0] + "物料不足");
                    }
                    //如果库存有物料就减少物料 （应该下单时减少）
                    matterEntity.setCurrentInventory(matterEntity.getCurrentInventory()-Integer.parseInt(s[1]));
                    matterService.updateById(matterEntity);
                    subscribeService.save(subscribe);


                }
                return R.ok();
            }
        }


        return R.error("没有此菜品");
    }
    /**
     * 保存
     */
//    @RequestMapping("/save")
////    @RequiresPermissions("menu:subscribe:save")
//    @Transactional
//    public R save(@RequestBody SubscribeEntity subscribe){
//        String idCard = subscribe.getIdCard();
//        //自动生成所需字段-当前时间
//
//        String TimeMillis = String.valueOf(System.currentTimeMillis());
//        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
//
//        //获取点菜的菜名
//        String dishName = subscribe.getDishName();
//
//
//        if (StringUtils.isNullOrEmpty(idCard) || " ".equals(idCard)){
//            return R.error("请输入身份证");
//        }
//        //建立关联数据
//        PersonDishEntity personDishEntity = new PersonDishEntity();
//        personDishEntity.setDishName(dishName);
//        personDishEntity.setCreatedAt(nowTime);
//        personDishEntity.setIdCard(idCard);
//        personDishService.save(personDishEntity);
//
//
//        subscribe.setSubCode(TimeMillis);
//        subscribe.setOrderTime( nowTime);
//
//        //到菜谱查询是否存在
//        MenuEntity menu = menuService.getOne(new QueryWrapper<MenuEntity>().eq("dish_name", dishName));
//        if (null != menu){
//            //获取物料
//            String reqMate = menu.getReqMate();
//            //查看所需物料
//            if (reqMate.contains(",") || reqMate.contains("_")) {
//                //分割
//                String[] rms = reqMate.split(",");
//                //遍历物料
//                for (String rm : rms) {
//                    //获取每一种物料
//                    String[] s = rm.split("_");
//                    //获取物料表
//                    MatterEntity matterEntity = matterService.getOne(new QueryWrapper<MatterEntity>().eq("mate_name", s[0]).ge("current_inventory", Integer.parseInt(s[1])));
//                    //如果是
//                    if (null == matterEntity) {
//                        return R.error(s[0] + "物料不足");
//                    }
//                //如果库存有物料就减少物料 （应该下单时减少）
//                matterEntity.setCurrentInventory(matterEntity.getCurrentInventory()-Integer.parseInt(s[1]));
//                matterService.updateById(matterEntity);
//                    subscribeService.save(subscribe);
//
//                    return R.ok();
//                }
//            }
//        }
//
//
//        return R.error("没有此菜品");
//    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("menu:subscribe:update")
    public R update(@RequestBody SubscribeEntity subscribe){
		subscribeService.updateById(subscribe);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("menu:subscribe:delete")
    public R delete(@RequestBody Integer[] ids){
		subscribeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
